package com.haowen.controller;

import com.haowen.domain.UserSong;
import com.haowen.service.UserSongService;
import com.haowen.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user-song")
public class UserSongController {

    @Autowired
    private UserSongService userSongService;

    /**
     * 上传音频文件
     */
    @PostMapping("/upload/song")
    public Object uploadSong(MultipartFile file) {
        Map<String, Object> result = new HashMap<>();
        if (file.isEmpty()) {
            result.put("code", 0);
            result.put("msg", "文件为空");
            return result;
        }
        try {
            String url = FileUtils.uploadSong(file);
            result.put("code", 1);
            result.put("url", url);
            return result;
        } catch (Exception e) {
            result.put("code", 0);
            result.put("msg", e.getMessage());
            return result;
        }
    }

    /**
     * 上传音频图片
     */
    @PostMapping("/upload/pic")
    public Object uploadPic(MultipartFile file) {
        Map<String, Object> result = new HashMap<>();
        if (file.isEmpty()) {
            result.put("code", 0);
            result.put("msg", "文件为空");
            return result;
        }
        try {
            String url = FileUtils.uploadPic(file);
            result.put("code", 1);
            result.put("url", url);
            return result;
        } catch (Exception e) {
            result.put("code", 0);
            result.put("msg", e.getMessage());
            return result;
        }
    }

    /**
     * 添加创作
     */
    @PostMapping("/add")
    public Object addSong(@RequestBody UserSong userSong) {
        Map<String, Object> result = new HashMap<>();
        try {
            userSong.setCreateTime(new Date());
            userSong.setUpdateTime(new Date());
            boolean flag = userSongService.addUserSong(userSong);
            if (flag) {
                result.put("code", 1);
                result.put("msg", "添加成功");
            } else {
                result.put("code", 0);
                result.put("msg", "添加失败");
            }
        } catch (Exception e) {
            result.put("code", 0);
            result.put("msg", "添加失败");
        }
        return result;
    }

    /**
     * 删除创作
     */
    @GetMapping("/delete")
    public Object deleteSong(Integer id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean flag = userSongService.deleteUserSong(id);
            if (flag) {
                result.put("code", 1);
                result.put("msg", "删除成功");
            } else {
                result.put("code", 0);
                result.put("msg", "删除失败");
            }
        } catch (Exception e) {
            result.put("code", 0);
            result.put("msg", "删除失败");
        }
        return result;
    }

    /**
     * 更新创作信息
     */
    @PostMapping("/update")
    public Object updateSong(@RequestBody UserSong userSong) {
        Map<String, Object> result = new HashMap<>();
        try {
            userSong.setUpdateTime(new Date());
            boolean flag = userSongService.updateUserSong(userSong);
            if (flag) {
                result.put("code", 1);
                result.put("msg", "更新成功");
            } else {
                result.put("code", 0);
                result.put("msg", "更新失败");
            }
        } catch (Exception e) {
            result.put("code", 0);
            result.put("msg", "更新失败");
        }
        return result;
    }

    /**
     * 获取用户创作列表
     */
    @GetMapping("/user/detail")
    public Object getUserSongs(Integer userId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<UserSong> list = userSongService.getUserSongByUserId(userId);
            result.put("code", 1);
            result.put("data", list);
        } catch (Exception e) {
            result.put("code", 0);
            result.put("msg", "获取失败");
        }
        return result;
    }
}